/**
 * 
 */
package com.sys.console.business.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.sys.console.business.SysUserService;
import com.sys.console.cookie.Constant_Cookie;
import com.sys.console.mapper.Sys_AuthorityMapper;
import com.sys.console.mapper.Sys_RoleMapper;
import com.sys.console.mapper.Sys_UserMapper;
import com.sys.console.pojo.Sys_Authority;
import com.sys.console.pojo.Sys_Role;
import com.sys.console.pojo.Sys_User;
import com.sys.console.pojo.Sys_UserUpdatePassword;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.page.PageBean;

/**
 * @author HS
 * 
 */
@Component
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	Sys_UserMapper sysUserMapper;
	@Autowired
	Sys_RoleMapper sysRoleMapper;
	@Autowired
	Sys_AuthorityMapper sysAuthorityMapper;

	public Sys_User findByName(String username) throws Exception {
		return sysUserMapper.selectByUserName(username);
	}

	@Override
	public Set<String> getRoles(Integer id) {
		ArrayList<Sys_Role> roles = sysRoleMapper.selectByUid(id);
		Set<String> set = new HashSet<String>();
		for (Sys_Role role : roles) {
			set.add(role.getTag());
		}
		return set;
	}

	@Override
	public Set<String> getAuthoritys(Integer rid) {
		ArrayList<Sys_Authority> authoritys = sysAuthorityMapper.selectByRid(rid);
		Set<String> set = new HashSet<String>();
		for (Sys_Authority authority : authoritys) {
			set.add(authority.getTag());
		}
		return set;
	}

	@Override
	public ArrayList<Sys_User> selectByList() throws Exception {
		// TODO Auto-generated method stub
		return sysUserMapper.selectByList();
	}

	@Override
	public int selectExistsByUsername(Sys_User record) throws Exception {
		// TODO Auto-generated method stub
		return sysUserMapper.selectExistsByUsername(record);
	}

	@Override
	public int saveSysUser(Sys_User record) throws Exception {
		if (record == null)
			throw new ObjException(ObjResultCode.DATA_ERROR);

		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		Sys_User user = (Sys_User) request.getSession().getAttribute(Constant_Cookie.CURRENT_USER_SESSION);
		if (record != null && record.getUid() != null && record.getUid() > 0) {
			record.setUtime(new Date());
			return sysUserMapper.updateByPrimaryKey(record);
		}
		record.setCtime(new Date());
		record.setUtime(new Date());

		String password = "";
		password = record.getPassword();

		record.setPassword(null);
		record.setPassword(DigestUtils.md5Hex(password));// 加密

		record.setIsadmin(0);
		record.setDreatorid(user.getUid()); // 创建者ID

		return sysUserMapper.insert(record);
	}

	@Override
	public String selectUserNameByUid(Integer uid) throws Exception {
		// TODO Auto-generated method stub
		return sysUserMapper.selectUserNameByUid(uid);
	}

	@Override
	public int updatePassword(Sys_UserUpdatePassword record) throws Exception {
		if (record == null)
			throw new ObjException(ObjResultCode.DATA_ERROR);

		String old_password = DigestUtils.md5Hex(record.getOld_password());
		String new_password = DigestUtils.md5Hex(record.getNew_password());
		String confirm_password = DigestUtils.md5Hex(record.getConfirm_password());
		// 数据库中原有密码
		String old_password2 = sysUserMapper.selectUserPasswordByUserId(record.getUid());

		if (!new_password.equals(confirm_password))
			throw new ObjException(ObjResultCode.USER_PASS_ERROR); // 输入的新密码与确认密码不一致

		if (!old_password.equals(old_password2))
			throw new ObjException(ObjResultCode.USER_OLDPASS_ERROR); // 原密码不正确

		record.setOld_password(old_password);
		record.setNew_password(new_password);

		return sysUserMapper.updatePassword(record);
	}

	@Override
	public int updateStatus(Sys_User record) throws Exception {
		if (record == null)
			throw new ObjException(ObjResultCode.DATA_ERROR);

		return sysUserMapper.updateStatus(record);
	}

	@Override
	public int updateLoginInfo(Sys_User record) throws Exception {
		if (record == null)
			throw new ObjException(ObjResultCode.DATA_ERROR);

		return sysUserMapper.updateLoginInfo(record);
	}

	@Override
	public Sys_User selectByAutoLogin(Sys_User record) throws Exception {
		// TODO Auto-generated method stub
		return sysUserMapper.selectByAutoLogin(record);
	}

	@Override
	public PageBean queryPageList(Sys_User entity, PageBean page) throws Exception {
		int rowCount = sysUserMapper.selectByPageCount(entity);
		page.setRowCount(rowCount);
		entity.setOffset(page.getStartRow());
		entity.setLimit(page.getPerpage());
		page.setList(sysUserMapper.selectByPage(entity));
		return page;
	}

	@Override
	public Sys_User queryByPrimaryKey(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return sysUserMapper.selectByPrimaryKey(id);
	}

	@Override
	public int saveOrUpdate(Sys_User entity) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int remove(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return sysUserMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int delete(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return sysUserMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updateStatus(String status, String[] ids) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateResetPass(Sys_UserUpdatePassword record) throws Exception {
		// TODO Auto-generated method stub
		record.setNew_password(DigestUtils.md5Hex(record.getNew_password()));
		return sysUserMapper.updateResetPassword(record);
	}

}
